<?php
declare(strict_types=1);

namespace app\common\model\video;

use app\common\model\BaseModel;

/**用户-课程模型
 * Class Course
 * @package app\common\model
 */
class UserCourse extends BaseModel
{
    protected $pk = 'id';
    protected $name = 'user_course';
    protected $updateTime = false;

    public function video(){
        $module = self::getCalledModule() ?: 'common';
        return $this->belongsTo("app\\{$module}\\model\\video\\Course",'c_id','id');
    }

    public function part(){
        $module = self::getCalledModule() ?: 'common';
        return $this->hasMany("app\\{$module}\\model\\video\\CoursePart",'c_id','c_id');
    }

    public function orderpart(){
        $module = self::getCalledModule() ?: 'common';
        return $this->hasMany("app\\common\\model\\video\\OrderPart",'user_course_id','id');
    }

    /**
     * @title 用户视频列表
     * @param
     * @param
     * @author Qian
     * Time: 2023/4/7  14:36
     */
    public function getList($param,$limit=10){
        $params = array_merge([
            'user_id'=>0,
            'status'=>''
        ],$param);
        $filter = [];
        $params['user_id'] > 0 && $filter['user_id'] = $params['user_id'];
        $params['status'] != '' && $filter['status'] = $params['status'];
        return $this->where($filter)->with(['video','video.image'])->withCount('part')->paginate($limit);
    }

    /**
     * @title 视频章节
     * @param $user_course_id
     * @param
     * @author Qian
     * Time: 2023/4/7  11:18
     */
    public function getCoursePart($user_course_id,$type = 1){
        if ($type == 1){
            //课程所有的章节
            return $this->where(['id'=>$user_course_id])->with(['video','part'=>['video','image']])->find();
        }else{
            //用户课程买的时候的章节
            return $this->where(['id'=>$user_course_id])->with(['orderpart'=>['part'=>['video','image']]])->find();
        }
    }
}
